/****************************************************************
 * <strong>MFSystem系统路信息类</strong>
 * <p>
 * 系统路信息获取的相关方法
 * </p>
 *
 * @author AndyZhang 2011
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.core;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.StringTokenizer;

public class MFSystem {
    public static String line = "\r\n";

    public static boolean isJar() throws Throwable {
        String path = MFSystem.class.getResource("").toString();
        return path.startsWith("jar");
    }

    public static int getRandom(int scope) throws Throwable {
        Random rm = new Random(scope);
        return rm.nextInt();

    }

    public static boolean isWindows() {
        return MFOSType.Windows == MFOSInfo.getOSType();
    }

    public static MFOSType getOSType() {
        return MFOSInfo.getOSType();
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return ip;

    }

    public static String getBrowser(HttpServletRequest request) {
        String userbrowser = "";
        try {
            String agent = request.getHeader("user-agent");
            StringTokenizer st = new StringTokenizer(agent, ";");
            st.nextToken();
            //得到用户的浏览器名
            userbrowser = st.nextToken();
        } catch (Exception e) {
            userbrowser = "unknow";
        }
        return userbrowser;
    }

    public static String getOS(HttpServletRequest request) {
        String agent = request.getHeader("user-agent");
        StringTokenizer st = new StringTokenizer(agent, ";");
        st.nextToken();
        String osVersion = "其它";
        String browserVersion = "其它";

        //得到用户的浏览器名
        String userbrowser = st.nextToken();

        //得到用户的操作系统
        String useros = st.nextToken();
        if (useros.indexOf("NT 6.0") > 0) {
            osVersion = "Windows Vista/Server 2008";
        } else if (useros.indexOf("NT 5.2") > 0) {
            osVersion = "Windows Server 2003";
        } else if (useros.indexOf("NT 5.1") > 0) {
            osVersion = "Windows XP";
        } else if (useros.indexOf("NT 5") > 0) {
            osVersion = "Windows 2000";
        } else if (useros.indexOf("NT 4") > 0) {
            osVersion = "Windows NT4";
        } else if (useros.indexOf("Me") > 0) {
            osVersion = "Windows Me";
        } else if (useros.indexOf("98") > 0) {
            osVersion = "Windows 98";
        } else if (useros.indexOf("95") > 0) {
            osVersion = "Windows 95";
        } else if (useros.indexOf("Mac") > 0) {
            osVersion = "Mac";
        } else if (useros.indexOf("Unix") > 0) {
            osVersion = "UNIX";
        } else if (useros.indexOf("Linux") > 0) {
            osVersion = "Linux";
        } else if (useros.indexOf("SunOS") > 0) {
            osVersion = "SunOS";
        }
        return osVersion;
    }

    public static String printStackTrace(Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return pw.toString();
    }

    /**
     * 获取IP地址
     *
     * @return
     * @throws UnknownHostException
     */
    public static String getIP() throws UnknownHostException {
        InetAddress localHost;
        localHost = InetAddress.getLocalHost();
        String ip = localHost.getHostAddress();
        return ip;
    }

    /**
     * 获取用户机器名称
     *
     * @return
     * @throws UnknownHostException
     */
    public static String getHostName() throws UnknownHostException {
        InetAddress localHost;
        localHost = InetAddress.getLocalHost();
        return localHost.getHostName();
    }


    /**
     * 获取C盘卷 序列号
     *
     * @return
     */
    public static String getDiskNumber() {
        if (isWindows()) {
            String line = "";
            String HdSerial = "";

            try {
                Process proces = Runtime.getRuntime().exec("cmd /c dir c:");
                BufferedReader buffreader = new BufferedReader(
                        new InputStreamReader(proces.getInputStream()));
                while ((line = buffreader.readLine()) != null) {
                    if (line.indexOf("卷的序列号是 ") != -1) {  //读取参数并获取硬盘序列号
                        HdSerial = line.substring(line.indexOf("卷的序列号是 ") + "卷的序列号是 ".length(), line.length());
                        break;
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return HdSerial;
        } else
            return "0000-0000";
    }

    /**
     * 获取Mac地址
     *
     * @return Mac地址，例如：F0-4D-A2-39-24-A6
     * @throws UnknownHostException
     */
    public static String getMac() throws UnknownHostException {
        InetAddress localHost;
        localHost = InetAddress.getLocalHost();
        NetworkInterface byInetAddress;
        try {
            byInetAddress = NetworkInterface.getByInetAddress(localHost);
            byte[] hardwareAddress = byInetAddress.getHardwareAddress();
            return getMacFromBytes(hardwareAddress);
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    private static String getMacFromBytes(byte[] bytes) {
        StringBuffer mac = new StringBuffer();
        byte currentByte;
        boolean first = false;
        for (byte b : bytes) {
            if (first) {
                mac.append("-");
            }
            currentByte = (byte) ((b & 240) >> 4);
            mac.append(Integer.toHexString(currentByte));
            currentByte = (byte) (b & 15);
            mac.append(Integer.toHexString(currentByte));
            first = true;
        }
        return mac.toString().toUpperCase();
    }


}
